package cn.edu.lsnu.mbti.config;

import cn.edu.lsnu.mbti.quartz.MyTask;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    //指定具体的定时任务类
    @Bean
    public JobDetail uploadTaskDetail() {
        JobDetail jobDetail = JobBuilder.newJob(MyTask.class)
                .withIdentity("MyTask")
                .storeDurably() //即使没有Trigger关联时，也不需要删除该JobDetail
                .build();
        return jobDetail;
    }

    @Bean
    public Trigger uploadTaskTrigger() {
        //这里设定执行方式 每隔一个小时去执行
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 */1 * * ?");
//        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("*/10 * * * * ?");
        // 返回任务触发器
        return TriggerBuilder.newTrigger()
                .forJob(uploadTaskDetail()) //关联上述的JobDetail
                .withIdentity("MyTask")
                .withSchedule(scheduleBuilder)
                .build();
    }
}
